package fun.sboy;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import fun.sboy.api.API;
import fun.sboy.jdbc.JDBCProperties;
import fun.sboy.listener.MouseCLickListener;
import fun.sboy.panel.LoginPanel;
import fun.sboy.panel.RegistryPanel;
import fun.sboy.pojo.ManagerUser;
import fun.sboy.util.EnterUtil;
import fun.sboy.util.IconUtil;
import fun.sboy.util.JDBCUtil;
import fun.sboy.view.HomeView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.io.*;
import java.rmi.registry.Registry;
import java.sql.Connection;
import java.util.Properties;

public class Main extends JFrame {

    private JPanel content = new JPanel();
    private LoginPanel loginPanel = new LoginPanel();
    private RegistryPanel registerPanel = new RegistryPanel(null);

    public Main() {

        //设置卡片布局
        CardLayout cardLayout = new CardLayout();
        content.setLayout(cardLayout);

        //登录处理
        addLoginBtnClickListener();
        addToRegisterLinkClickListener();
        content.add(loginPanel, "loginPanel");
        //注册处理
        addToLoginLinkClickListener();
        content.add(registerPanel, "registerPanel");
        cardLayout.show(content, "loginPanel");
        content.setBackground(Color.pink);
        add(content, BorderLayout.CENTER);


        //其他设置
        setSize(450, 355);
        setIconImage(IconUtil.load("logo", 100, 100, 1));
        setTitle("Login");
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void addToLoginLinkClickListener() {
        JLabel toLoginPanel = registerPanel.toLoginPanel;
        toLoginPanel.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                CardLayout cardLayout = (CardLayout) content.getLayout();
                cardLayout.show(content, "loginPanel");
                setTitle("Login");
            }
        });
    }

    private void addToRegisterLinkClickListener() {
        JLabel toRegisterLink = loginPanel.toRegisterLink;

        toRegisterLink.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                CardLayout layout = (CardLayout) content.getLayout();
                layout.show(content, "registerPanel");
                setTitle("Register");
            }
        });
    }

    private void addLoginBtnClickListener() {
        //登录页面
        JTextField passwordInput = loginPanel.passwordInput;
        JTextField usernameInput = loginPanel.usernameInput;
        loginPanel.loginBtn.addMouseListener(new MouseCLickListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String username = usernameInput.getText();
                String password = passwordInput.getText();
                if (username.equals("") || password.equals("")) {
                    JOptionPane.showMessageDialog(null, "账号或密码不能为空！！！", "WARNING", JOptionPane.WARNING_MESSAGE);
                } else {
                    if (!API.login(username, password)) {
                        JOptionPane.showMessageDialog(null, "账号或密码不正确！！！", "WARNING", JOptionPane.WARNING_MESSAGE);
                    } else {
                        new HomeView();
                        dispose();
                    }
                }
            }
        });
    }


    public static void main(String[] args) {

        String path = System.getProperty("user.dir");
        File file = new File(path + "/dataSource.properties");
        System.out.println(file.getAbsolutePath());
        if (!file.exists()) {
            JOptionPane.showMessageDialog(null, "没有数据源配置文件，默认为您创建了文件在jar包目录文件下；请配置！！！", "未初始化Mysql数据源", JOptionPane.ERROR_MESSAGE);
            Properties properties = new Properties();
            properties.setProperty("数据库名", "填写数据库的名字");
            properties.setProperty("用户名", "数据库登录的用户名");
            properties.setProperty("密码", "数据库登录的密码");
            try {
                FileWriter writer = new FileWriter(file);
                properties.store(writer, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Properties properties = new Properties();
            try {
                properties.load(new FileReader(file));
                JDBCProperties.databaseName = properties.getProperty("数据库名");
                JDBCProperties.username = properties.getProperty("用户名");
                JDBCProperties.password = properties.getProperty("密码");
                new Thread(() -> {
                    JOptionPane.showMessageDialog(null, "正在尝试连接数据库......", "正在尝试", JOptionPane.INFORMATION_MESSAGE);
                }).start();
                initDataSource();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Connection connection = JDBCUtil.getConnection();
            if (connection == null) {
                new Thread(() -> {
                    JOptionPane.showMessageDialog(null, "Mysql数据源配置文件配置错误，请检查配置！！！", "Mysql配置源错误", JOptionPane.ERROR_MESSAGE);
                }).start();
            } else {
                JDBCUtil.closeSource(connection, null, null);
                new Main();
            }
        }
    }


    public static void initDataSource() {
        Properties properties = JDBCProperties.getProperties();
        try {
            JDBCUtil.dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            new Thread(() -> {
                JOptionPane.showMessageDialog(null, "Mysql数据源配置文件配置错误，请检查配置！！！", "Mysql配置源错误", JOptionPane.ERROR_MESSAGE);
            }).start();
        }
    }


}